#include <string>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include "compare.h"
#include "db.h"
#include "table.h"

std::vector<int> compare(std::vector<std::string> strs,int index){
   
    std::vector<int> datas = {};
     //找到WHERE中的操作的列
    int col_chosen = -1;
    for(int i = 0 ; i < all_table[index].col_num;i++){
            if(all_table[index].column[i] == strs[0]) col_chosen = i;
    }
    if(col_chosen == -1){
        std::cout << "该列不存在 " + strs[0] << std::endl;
        return datas;
    }

    if(all_table[index].type[col_chosen] == "string" && strs[1] != "="){
        std::cout << "String类型仅支持“=” 运算符 " + strs[0] << std::endl;
        return datas;
    }

    //string类型的 =
    if(all_table[index].type[col_chosen] == "string" && strs[1] == "="){
         for (int i = 0; i < all_table[index].row_num; i++){
                if(table_data[i][col_chosen].compare(strs[2]) == 0 ) datas.push_back(i);
            }
        return datas;
    }
    //int类型的 >
    if(all_table[index].type[col_chosen] == "int" && strs[1] == ">"){
         for (int i = 0; i < all_table[index].row_num; i++){
                if(atoi(table_data[i][col_chosen].c_str()) > atoi(strs[2].c_str())) datas.push_back(i);
            }
        return datas;
    }
    //int =
    if(all_table[index].type[col_chosen] == "int" && strs[1] == "="){
         for (int i = 0; i < all_table[index].row_num; i++){
                if(atoi(table_data[i][col_chosen].c_str()) == atoi(strs[2].c_str())) datas.push_back(i);
            }
        return datas;
    }

    //int <
    if(all_table[index].type[col_chosen] == "int" && strs[1] == "<"){
         for (int i = 0; i < all_table[index].row_num; i++){
                if(atoi(table_data[i][col_chosen].c_str()) < atoi(strs[2].c_str())) datas.push_back(i);
            }
        return datas;
    }

    return datas;

}